import axios from 'axios';
const CancelToken = axios.CancelToken;

let _pendingList = [];

// 取消全部正在请求的接口
export function clearAllReq() {
  if (_pendingList.length > 0) {
    _pendingList.forEach(v => {
      v.cancel({
        config: {
          url: v.url,
        },
        message: 'Cancel Request',
      });
    });
  }
}

const instance = axios.create({
    // baseURL: "https://steel.melon6.com/api",
    baseURL: globalConfig['BASE_URL'],
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    timeout: 300000,
});

/* axios请求拦截 */
instance.interceptors.request.use(
  config => {
    const t = new Date().getTime();
    config.url += config.url.includes('?') ? `&t=${t}` : `?t=${t}`;
    config.cancelToken = new CancelToken(cancel => {
      _pendingList.push({
        url: config.url,
        cancel,
        loading: config.notLoading ? false : true,
      });
    });
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

/* axios响应拦截 */
instance.interceptors.response.use(
  response => {
    // 从列表中移除请求完成的接口
    if (_pendingList.some(v => v.url === response.config.url)) {
      _pendingList = _pendingList.filter(v => v.url !== response.config.url);
    }
    return response;
  },
  err => {
    if (error.config) {
      _pendingList = _pendingList.filter(v => v.url !== error.config.url);
    }
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = '请求错误(400)';
          break;
        case 401:
          err.message = '未授权，请重新登录(401)';
          break;
        case 403:
          err.message = '拒绝访问(403)';
          break;
        case 404:
          err.message = '请求出错(404)';
          break;
        case 408:
          err.message = '请求超时(408)';
          break;
        case 500:
          err.message = '服务器错误(500)';
          break;
        case 501:
          err.message = '服务未实现(501)';
          break;
        case 502:
          err.message = '网络错误(502)';
          break;
        case 503:
          err.message = '服务不可用(503)';
          break;
        case 504:
          err.message = '网络超时(504)';
          break;
        case 505:
          err.message = 'HTTP版本不受支持(505)';
          break;
        default:
          err.message = `连接出错(${err.response.status})!`;
      };
    } else {
      err.message = '服务器连接失败';
    };
    return Promise.reject(err);
  }
);

const http = {
  /* get */
  get:(url,params,options) => {
    return new Promise((resolve,reject) => {
      instance.get(url,{params,...options}).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      });
    });
  },
  /* post */
  post:(url,params,options={}) => {
    return new Promise((resolve, reject) => {
      instance.post(url, params, options).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      });
    });
  },
  /* put */
  put:(url,params,options={}) => {
    return new Promise((resolve, reject) => {
      instance.post(url, params, options).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      });
    });
  },
  /* delete */
  delete:(url,params,options={}) => {
    return new Promise((resolve,reject) => {
      instance.post(url,params,options).then(res => {
        resolve(res);
      }).catch(err => {
        reject(err);
      });
    });
  }
};

export default http;